
QFaces.Faces = {
	suffix : null,		// QFaces后缀配匹，只配匹后缀
	contextPath : null,
	viewId : null,		// 
	loadingSrc : null,	// 图标地址“loading”
	uis : new Array(),	// 包含所有组件
	put : function(clientId, UI) {
		this.uis[clientId] = UI;
		if (clientId.indexOf(":") != -1) {
			var shortId = clientId.substring(clientId.lastIndexOf(":") + 1);
			this.uis[shortId] = UI;
		}
	},
	find : function(clientId) {
		return this.uis[clientId];
	}
}
QFaces.Faces.Finder = {
	find : function(clientId) {return QFaces.Faces.find(clientId);},
	put : function(clientId, UI) {QFaces.Faces.put(clientId, UI);}
}
QFaces.Faces.Ajax = {
	ajax : new Array(),	// 存放并管理所有组件的ajax请求信息
	actionURI : null,	// 用于保存请求地址的URL
	getAjax : function(clientId) {
		if (this.ajax[clientId] == null) {
			var processor = new QFaces.Faces.Ajax.Processor(clientId, QFaces.Utils.getXmlRequest());
			this.ajax[clientId] = processor;
		}
		return this.ajax[clientId];
	},
	getViewState : function() {
		var encodedState;
		var stateElements = window.document.getElementsByName("javax.faces.ViewState");
		var stateValue = null;
		if (null != stateElements && 0 < stateElements.length) {
			stateValue = stateElements[0].value;
			var uriEncodedState = encodeURI(stateValue);
			var rexp = new RegExp("\\+", "g");
			encodedState = uriEncodedState.replace(rexp, "\%2B");
		}
		return encodedState;
	},
	getActionURI : function() {
		if (this.actionURI != null) return this.actionURI;
		this.actionURI = QFaces.Faces.contextPath + QFaces.Faces.viewId + "";
		if (this.actionURI.indexOf(".") != -1) {
			this.actionURI = this.actionURI.substring(0, this.actionURI.lastIndexOf(".")) + "." + QFaces.Faces.suffix;
		} else {
			this.actionURI = this.actionURI + "." + QFaces.Faces.suffix;
		}
		return this.actionURI;
	},
	onreadyStatechange : function(clientId) {
		var processor = QFaces.Faces.Ajax.getAjax(clientId);
		// 这个方法里面不可能出现readyState == 0,存在的情况只有被多个函数同时调用时出现的冲突.
		if (processor.request.readyState == 0) {
			QFaces.debug("Ajax,未初始化,返回, readyState=" + processor.request.readyState);
			return;
		} else if (processor.request.readyState == 1) {
			QFaces.debug("Ajax,请求已经建立, 未发送, readyState=" + processor.request.readyState);
			QFaces.Utils.setTimeout(QFaces.Faces.Ajax.onreadyStatechange, 20, clientId);
		} else if (processor.request.readyState == 2) {
			QFaces.debug("Ajax,请求已发送，正在处理中, readyState=" + processor.request.readyState);
			QFaces.Utils.setTimeout(QFaces.Faces.Ajax.onreadyStatechange, 20, clientId);
		} else if (processor.request.readyState == 3) {
			QFaces.debug("Ajax,请求在处理中,未完全响应, readyState=" + processor.request.readyState);
			QFaces.Utils.setTimeout(QFaces.Faces.Ajax.onreadyStatechange, 20, clientId);
		} else if (processor.request.readyState == 4) {
			QFaces.debug("Ajax,OK, 响应已经完成,readyState = " + processor.request.readyState);
			if (processor.request.status == 200) {
				QFaces.debug("Ajax,status:" + processor.request.status + ", 请求成功.");
				processor.process.apply();
				QFaces.Faces.Ajax.ajax[clientId] = null;
			} else if (processor.request.status == 401) {
				QFaces.debug("Ajax,Error:" + processor.request.status + ", 未经授权");
			} else if (processor.request.status == 403) {
				QFaces.debug("Ajax,Error:" + processor.request.status + ", 禁止");
			} else if (processor.request.status == 404) {
				QFaces.debug("Ajax,Error:" + processor.request.status + ", 没找到资源");
			} else if (processor.request.status == 500) {
				QFaces.debug("Ajax,Error:" + processor.request.status);
				QFaces.debug("Ajax,Error, Message=:" + processor.request.responseText);
			}
		} else {
			QFaces.debug("Ajax,Error,未知的状态类型, readyState=" + processor.request.readyState);
		}
	}
}
QFaces.Faces.Ajax.Processor = function(clientId, requestAjax) {
	this.clientId = clientId;
	this.request = requestAjax;
	this.param = new Array();
	this.process = null;
	this.put = function(key, value) {
		var map = new Array(2);
		map[0] = key;
		map[1] = value;
		this.param.push(map);
	}
	this.getValues = function() {return this.baseValue(this.param);}
	this.postValues = function() {return this.baseValue(this.param) + "javax.faces.ViewState=" + QFaces.Faces.Ajax.getViewState();}
	this.baseValue = function(array) {
		var temp = "";
		for (var i = 0; i < array.length; i++) {
			var map = array[i];
			temp += map[0] + "=" + this.encodeValue(map[1]) + "&";
		}
		return temp;
	}
	this.encodeValue = function(val) {
		var _val = encodeURI(val);
		var rexp1 = new RegExp("\\+", "g"); // +
		var rexp2 = new RegExp("&", "g");	// &
		_val = _val.replace(rexp1, "\%2B");
		_val = _val.replace(rexp2, "\%26");
		return _val;
	}
	this.setProcess = function(fun) {this.process = fun;}
	this.preSend = function() {
		this.request.open("POST", QFaces.Faces.Ajax.getActionURI(), true);
		this.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
		try {
			this.request.onreadyStatechange = QFaces.Faces.Ajax.onreadyStatechange(this.clientId);
		} catch (e) {
			QFaces.debug("Error=" + e);
		} // Catch exception when use ie6 (There are any errors occur when use ie6)
	}
	this.get = function() {
		this.preSend();
		this.request.send(this.getValues());
	}
	this.post = function() {
		this.preSend();
		this.request.send(this.postValues());
	}
}
QFaces.Faces.Messenger = {
	flag : null,
	init : false,
	getLoading : function() {
		if (!this.init) this.toInit();
		this.flag.style.display = "";
		return this.flag;
	},
	getLoadingSign : function() {return this.getLoading();},
	processing : function(process, obj) {
		if (!this.init) this.toInit();
		this.flag.style.display = (process? "" : "none");
		this.flag.style.zIndex = QFaces.Utils.zIndex++;
		this.flag.style.position = "absolute";
		var _obj = QFaces.Utils.getElement(obj);
		var point = QFaces.Utils.getClientPosition(_obj);
		if (point == null) 
			point = QFaces.Utils.getMousePosition();
		if (point != null) {
			this.flag.style.position = "absolute";
			this.flag.style.left = point.x + "px";
			this.flag.style.top = point.y + "px";
			return;
		}
	},
	toInit : function() {
		if (this.init) return;
		this.init = true;
		this.flag = document.createElement("img");
		this.flag.setAttribute("src", QFaces.Faces.loadingSrc);
		this.flag.style.display = "none";
		document.body.appendChild(this.flag);
	}
}
QFaces.toAccDisplay = function(id, direction) {
	var obj = QFaces.Utils.getElement(id);
	if (!obj) {
		QFaces.Utils.accDisplay(obj, "show", (direction ? direction : "height"));
	} else {
		if (obj.style.display == "none") {
			QFaces.Utils.accDisplay(obj, "show", (direction ? direction : "height"));
		} else {
			QFaces.Utils.accDisplay(obj, "hide", (direction ? direction : "height"));
		}
	}
}